package com.yj_app.qualitytest.activity.score;

import android.os.Bundle;
import android.text.TextUtils;
import android.util.Log;
import android.util.SparseArray;
import android.view.View;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentManager;
import androidx.fragment.app.FragmentTransaction;
import androidx.viewpager.widget.ViewPager;

import com.google.android.material.tabs.TabLayout;
import com.hu_sir.sweet_dialog_reversion.SweetAlertDialog;
import com.lzy.okgo.OkGo;
import com.lzy.okgo.adapter.Call;
import com.lzy.okgo.callback.Callback;
import com.lzy.okgo.callback.StringCallback;
import com.lzy.okgo.model.Progress;
import com.lzy.okgo.model.Response;
import com.lzy.okgo.request.base.BodyRequest;
import com.lzy.okgo.request.base.Request;
import com.yj_app.qualitytest.R;
import com.yj_app.qualitytest.activity.scoredetail.ScoreDetailActivity;
import com.yj_app.qualitytest.adapter.ViewPagerAdapter;
import com.yj_app.qualitytest.base.BaseActivity;
import com.yj_app.qualitytest.callback.DialogCallback;
import com.yj_app.qualitytest.common.Config;
import com.yj_app.qualitytest.common.NetConfig;
import com.yj_app.qualitytest.fragment.BiaoChengFragment;
import com.yj_app.qualitytest.fragment.FacadeFragment;
import com.yj_app.qualitytest.fragment.HouDuFragment;
import com.yj_app.qualitytest.fragment.NianKouFragment;
import com.yj_app.qualitytest.fragment.SanitationFragment;
import com.yj_app.qualitytest.fragment.TestFragment;
import com.yj_app.qualitytest.info.BcInfo;
import com.yj_app.qualitytest.info.HdInfo;
import com.yj_app.qualitytest.info.Loginfo;
import com.yj_app.qualitytest.info.SubScoreInfo;
import com.yj_app.qualitytest.info.TeamInfo;
import com.yj_app.qualitytest.info.up.MsgBean;
import com.yj_app.qualitytest.info.up.QualityVO;
import com.yj_app.qualitytest.info.up.WsConstant;
import com.yj_app.qualitytest.util.DateUtils;
import com.yj_app.qualitytest.util.GsonUtil;
import com.yj_app.qualitytest.util.PreferencesManager;

import org.litepal.LitePal;

import java.io.InputStream;
import java.io.OutputStream;
import java.lang.reflect.Field;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.MediaType;

/**
 * Copyright (C), 2015-2019/9/12, 石家庄云捷软件科技有限公司
 *
 * @ProjectName: ScoreActivity.java
 * @Package: com.yj_app.qualitytest.activity.score
 * @ClassName: ScoreActivity
 * @Description: TODO
 * @Author: Hu_Sir
 * @CreateDate: 2019/9/12--11:27
 * @UpdateUser: ?
 * @UpdateDate: 2019/9/12--11:27
 * @UpdateRemark: todo
 */
public class ScoreActivity extends BaseActivity {
    int flag;
    TeamInfo teamInfo;
    Loginfo loginfo;
    @BindView(R.id.tabs)
    TabLayout mTabs;
    @BindView(R.id.my_vp)
    ViewPager mMyVp;
    String[] guamiantitle = {"外观", "卫生", "厚度", "粘口", "标称"};
    String[] zhifentitle = {"外观", "卫生", "封口", "标称"};
    String[] wathertitle = {"外观", "卫生"};

    List<Fragment> fragmentList;
    ViewPagerAdapter viewPagerAdapter;
    Bundle bundle;
    //    SweetAlertDialog sweetAlertDialog;
    FragmentTransaction fragmentTransaction;
    FragmentManager fragmentManager;
    FacadeFragment fc;
    SanitationFragment ws;
    HouDuFragment hd;
    NianKouFragment nk;
    BiaoChengFragment bk;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_score);
        ButterKnife.bind(this);
        initView();
        initEvent();
        initViewpager();

//        if (!hasSj()) {
//            toastInfo("您有未上传的数据，请先上传。");
//            //todo 上传数据
//            sweetAlertDialog = new SweetAlertDialog(this, SweetAlertDialog.NORMAL_TYPE);
//            sweetAlertDialog.setTitleText("上传数据");
//            sweetAlertDialog.setContentText("请先上传后再进行操作");
//            sweetAlertDialog.setConfirmText("确定").setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
//                @Override
//                public void onClick(SweetAlertDialog sweetAlertDialog) {
//                    toastSuccess("上传中……，请稍后！");
//                    sweetAlertDialog.dismiss();
//                }
//            });
//            sweetAlertDialog.setCancelable(false);
//            sweetAlertDialog.show();
//            return;
//        }
    }


    private void initEvent() {
        seeDetail.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                startActivity(ScoreDetailActivity.class, bundle);
            }
        });
    }

    private void initViewpager() {
        viewPagerAdapter = new ViewPagerAdapter(fragmentManager, fragmentList, flag == 1 ? guamiantitle : flag == 2 ? zhifentitle : wathertitle);
        mMyVp.setAdapter(viewPagerAdapter);
        mTabs.setupWithViewPager(mMyVp);
    }

    private void initView() {
        fragmentManager = getSupportFragmentManager();
        fragmentTransaction = fragmentManager.beginTransaction();
        bundle = getIntent().getExtras();
        teamInfo = (TeamInfo) bundle.getSerializable(Config.TEAM_INFO);
        loginfo = PreferencesManager.getInstance(this).get(Config.LOGINFO, Loginfo.class);
        flag = bundle.getInt(Config.FLAG);
        mTabs.setTabMode(TabLayout.MODE_FIXED);
        mTabs.setTabGravity(TabLayout.GRAVITY_FILL);
        fragmentList = new ArrayList<>();
        addFragment();

        setTitleWithBack("班次：" + teamInfo.getGname() + "\t\t\t检查日期：" + DateUtils.convertToString(System.currentTimeMillis()) + "\t\t\t" + "操作员：" + loginfo.getNickname());
        seeDetail.setVisibility(View.VISIBLE);
        mTabs.setVisibility(View.VISIBLE);
    }

    private void addFragment() {
        fc = FacadeFragment.getIntance().setBundle(bundle);
        ws = SanitationFragment.getIntance().setBundle(bundle);
        hd = HouDuFragment.getIntance().setBundle(bundle);
        nk = NianKouFragment.getIntance().setBundle(bundle);
        bk = BiaoChengFragment.getIntance().setBundle(bundle);
        //外观
        fragmentList.add(fc);
        //卫生
        fragmentList.add(ws);
        //厚度
        if (flag == 1)
            fragmentList.add(hd);
        if (flag == 1 || flag == 2) {

            //封口
            fragmentList.add(nk);
            //标称
            fragmentList.add(bk);
        }
    }


}
